package com.zrrd.mcl.payment.controller;

import com.alipay.api.AlipayApiException;
import com.zrrd.mcl.common.result.Result;
import com.zrrd.mcl.model.order.OrderInfo;
import com.zrrd.mcl.order.client.OrderFeignClient;
import com.zrrd.mcl.payment.service.AlipayService;
import com.zrrd.mcl.payment.service.PaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/payment/alipay")
public class AlipayController {

    @Autowired
    private PaymentInfoService paymentInfoService;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private AlipayService alipayService;

    //生成支付记录
    @RequestMapping("/savePaymentInfo/{orderId}")
    public Result savePaymentInfo(@PathVariable("orderId") Long orderId) {
        //先通过feign请求订单服务获取订单
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
        //调用业务层保存记录
        paymentInfoService.savePaymentInfo(orderInfo,"支付宝");
        return Result.ok();
    }

    //提交支付
    @RequestMapping("/submit/{orderId}")
    public String submit(@PathVariable("orderId") Long orderId) throws AlipayApiException {
        String from = alipayService.createPayCode(orderId);
        return from;
    }

}
